$().ready(function () {
    validateRule();
});

$.validator.setDefaults({
    submitHandler: function () {
        save();
    }
});

//选择机构对应证书
$("#orgId").change(function () {
    $("#profileId").empty();
    $("#userId").empty();
    $.ajax({
        url: '/jzweb/courses/selPros',
        data: {orgIds: $(this).val()},
        success: function (r) {
            if (r.data == null) {
                $("#profileId").append('<option value="">暂无数据</option>');
            } else {
                $("#profileId").append('<option value="">请选择</option>');
                for (var p in r.data) {
                    $("#profileId").append('<option value="' + r.data[p].id + '">' + r.data[p].name + '</option>');
                }
            }

        }
    })
});


//讲师
$("#profileId").change(function () {
    $("#teacherId").empty();
    $.ajax({
        url: '/jzweb/courses/selTeacher',
        data: {orgIds: $('#orgId').val(), cprofileIds: $(this).val()},
        success: function (r) {
            console.log("er", r)
            if (r.data.length == 0) {
                $("#teacherId").append('<option value="">暂无数据</option>');
            } else {
                $("#teacherId").append('<option value="">请选择</option>');
                for (var p in r.data) {
                    $("#teacherId").append('<option value="' + r.data[p].id + '">' + r.data[p].name + '</option>');
                }
            }

        }
    })
});
//课程
$("#teacherId").change(function () {
    $("#courseId").empty();
    $.ajax({
        url: '/jzweb/courses/selCoureses',
        data: {orgIds: $('#orgId').val(), cprofileIds: $('#profileId').val(), teacherIds: $(this).val()},
        success: function (r) {
            console.log("er", r)
            if (r.data.length == 0) {
                $("#courseId").append('<option value="">暂无数据</option>');
            } else {
                $("#courseId").append('<option value="">请选择</option>');
                for (var p in r.data) {
                    $("#courseId").append('<option value="' + r.data[p].id + '">' + r.data[p].name + '</option>');
                }
            }
        }
    })
});

function save() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "/jzweb/coursesClass/save",
        data: $('#signupForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            orgId: {
                required: true
            },
            profileId: {
                required: true
            },
            teacherId: {
                required: true
            },
            courseId: {
                required: true
            },
            studentClassId: {
                required: true
            },
        },
        messages: {}
    })
}

